<template>
	<div class="system-add-dept-container">
		<el-drawer title="修改权限表" v-model="showWindows" @close="onCancel">
			<el-form ref="formRef" :model="sysRole" :rules="rules" size="small" label-width="30%" style="width: 90%">
				<el-form-item label="名称" prop="name">
					<el-input v-model="sysRole.name" placeholder="请输入名称" />
				</el-form-item>
				<el-form-item label="编码" prop="code">
					<el-input v-model="sysRole.code" placeholder="请输入编码" />
				</el-form-item>
				<el-form-item label="数据权限" prop="dataScope">
					<el-select v-model="sysRole.dataScope" style="width: 100%">
						<el-option :value="'ALL'" label="全部" />
						<el-option :value="'THIS_LEVEL_CHILDREN'" label="本级以及子级" />
						<el-option :value="'THIS_LEVEL'" label="本级" />
						<el-option :value="'CUSTOMIZE'" label="自定义" />
						<el-option :value="'SELF'" label="个人" />
					</el-select>
				</el-form-item>
				<el-form-item label="自定义数据权限" prop="customDataScope">
					<el-select v-model="sysRole.dataScope" style="width: 100%">
						<el-option :value="'ALL'" label="全部" />
						<el-option :value="'THIS_LEVEL_CHILDREN'" label="本级以及子级" />
						<el-option :value="'THIS_LEVEL'" label="本级" />
						<el-option :value="'CUSTOMIZE'" label="自定义" />
						<el-option :value="'SELF'" label="个人" />
					</el-select>
				</el-form-item>
				<el-form-item label="描述" prop="describe">
					<el-input v-model="sysRole.describe" placeholder="请输入描述" />
				</el-form-item>
			</el-form>
			<div class="el-drawer__footer">
				<el-button @click="onCancel" size="small">取 消</el-button>
				<el-button type="primary" @click="onSubmit" size="small">确 定</el-button>
			</div>
		</el-drawer>
	</div>
</template>

<script lang="ts">
import { reactive, ref, toRefs } from 'vue';
import { editSysRole, getSysRoleInfo } from '/@/api/organized/role/index';
import { ElMessage } from 'element-plus';

export default {
	name: 'editSysRole',
	emits: ['initTableData'],
	props: {},
	setup(props, context) {
		const formRef = ref();

		const state = reactive({
			showWindows: false,
			sysRole: {
				name: '',
				code: '',
				describe: '',
				dataScope: '',
				customDataScope: [],
			},
			roleId: '',
			rules: {
				name: [{ required: true, message: '请输入名称', trigger: 'blue' }],
				code: [{ required: true, message: '请输入编码', trigger: 'blue' }],
				dataScope: [{ required: true, message: '请输入数据权限', trigger: 'blue' }],
			},
		});

		//打开添加窗口
		const openWindows = (roleId: any) => {
			state.roleId = roleId;
			getSysRoleInfo(roleId).then((res: any) => {
				state.sysRole = res.data;
			});
			state.showWindows = true;
		};

		//取消
		const onCancel = () => {
			formRef.value.resetFields();
			state.showWindows = false;
		};

		//提交
		const onSubmit = () => {
			formRef.value.validate((valid: any) => {
				if (valid) {
					editSysRole(state.sysRole, state.roleId).then((res: any) => {
						if (res.code == 200) {
							onCancel();
							context.emit('initTableData');
						} else {
							ElMessage.error(res.message);
						}
					});
				}
			});
		};

		return {
			formRef,
			openWindows,
			onCancel,
			onSubmit,
			...toRefs(state),
		};
	},
};
</script>

<style scoped>
.cropper-img {
	margin: auto;
	width: 100px;
	height: 100px;
	border-radius: 100%;
}
</style>
